package com.u2b.db.status;

import java.util.HashMap;
import java.util.List;

import orm.AddProxy;
import orm.QueryProxy;
import orm.SqlCreate;
import orm.UpdateProxy;

public class StatusDaoImpl
{
    private final QueryProxy queryProxy;

    public StatusDaoImpl()
    {
        queryProxy = new QueryProxy();
    }

    public int addStatus(HashMap<String, Object> value)
    {
        int primaryKey = 0;
        String sql = SqlCreate.createInsertSql("status_db", value);
        String[] sqlKeys = SqlCreate.createSqlKeys(value);

        // String sql =
        // "insert into status_db(owner,extra_message,photo_URL,audio_URL,video_URL,create_time,latitude,longitude) values(?,?,?,?,?,?,?,?)";
        // String[] sqlKeys = new String[] { "owner", "extra_message",
        // "photo_URL","audio_URL", "video_URL", "create_time",
        // "latitude","longitude" };
        primaryKey = new AddProxy().add(sql, sqlKeys, value);
        return primaryKey;

    }
       /**
        *  得到数据库中间的状态,分页显示.有两个需要改进的地方:
        *  1.limit实现分页可能不太好,不知道有没有好的方式(比如缓存等)
        *  2.得到的结果是按照时间排序,并没有体现附近的距离.需要改正
        * @param page 页码
        * @return List<HashMap<String, Object>>
        */
    public List<HashMap<String, Object>> getLastStatus(int page)
    {
        int offset = (page - 1) * 10;
        System.out.println("offset" + offset);

        String sql = "select user_volatile_db.user_name,user_volatile_db.head_URL,status_db.* from user_volatile_db,status_db where user_volatile_db.uid in(select owner from status_db)  order by  sid desc limit ?,10";

        List<HashMap<String, Object>> result = new QueryProxy().query(sql, offset);
        return result;

    }

    public int commentThis(HashMap<String, Object> value)
    {
        String sql = "insert into status_comment_message_db(sid,uid,create_time,comment_content,refrence_mid) values(?,?,?,?,?)";
        String[] keys = new String[]
        { "sid", "uid", "create_time", "comment_content", "refrence_mid" };
        int primaryKey = new AddProxy().add(sql, keys, value);
        if (primaryKey > 0)
        {
            String sid = (String) value.get("sid");
            String sql_s = "update status_db set current_comment_num=current_comment_num+1 where sid=" + sid;
            new UpdateProxy().update(sql_s);
        }
        return primaryKey;
    }

    public List<HashMap<String, Object>> getDetailedMessage(int sid)
    {
        String sql = "select user_volatile_db.user_name,user_volatile_db.head_URL, status_comment_message_db.* from user_volatile_db,status_comment_message_db where sid=? and user_volatile_db.uid in (status_comment_message_db.uid)";
        List<HashMap<String, Object>> result = new QueryProxy().query(sql, sid);
        return result;
    }

}